package com.example.shopping_li.me;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.shopping_li.Fragment.BaseFragment;
import com.example.shopping_li.MainActivity;
import com.example.shopping_li.R;
import com.example.shopping_li.URLUtils;
import com.example.shopping_li.utils.CodeUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;


public class RegFragment extends BaseFragment implements View.OnClickListener {
EditText nameEt,pwdEt,repwdEt,telEt;
Button regBtn,btn_yan;
EditText et_phoneCodes;
ImageView image;
    private Bitmap bitmap;
    private String code;
   String Code;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
     View view=inflater.inflate(R.layout.fragment_reg, container, false);
       initView(view);
        return view;
    }

    private void initView(View v) {
        nameEt = v.findViewById(R.id.frag_reg_et2);
        pwdEt = v.findViewById(R.id.frag_reg_et3);
        repwdEt = v.findViewById(R.id.frag_reg_et4);
        telEt = v.findViewById(R.id.frag_reg_et1);
        regBtn = v.findViewById(R.id.frag_reg_btn);
        btn_yan=v.findViewById(R.id.btn_yan);
        image=v.findViewById(R.id.image);
         et_phoneCodes=v.findViewById(R.id.et_phoneCodes);
        btn_yan.setOnClickListener(this);
        regBtn.setOnClickListener(this);
        image.setOnClickListener(this);
        //获取工具类生成的图片验证码对象
        bitmap = CodeUtils.getInstance().createBitmap();
        //获取当前图片验证码的对应内容用于校验
        code = CodeUtils.getInstance().getCode();
        image.setImageBitmap(bitmap);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.frag_reg_btn:
                String nickname = nameEt.getText().toString();//获取昵称
                String tel=telEt.getText().toString();//获取电话
                String pwd=pwdEt.getText().toString();//获取密码
                String repwd=repwdEt.getText().toString();//获取确认密码

                if (!checkInfo(nickname, tel, pwd, repwd)) return;

                Map<String,String>map=new HashMap<>();
                map.put("phone",tel);
                map.put("password",pwd);
                map.put("nickname",nickname);
                map.put("name",tel);
                map.put("address",pwd);
                map.put("question",nickname);
                map.put("answer",nickname);


                //联网
                postNetwork(URLUtils.regURL,map);
                break;
            case R.id.image:

                bitmap = CodeUtils.getInstance().createBitmap();
                code = CodeUtils.getInstance().getCode();
                image.setImageBitmap(bitmap);
                Toast.makeText(getContext(), code, Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_yan:

                Code=et_phoneCodes.getText().toString();
                et_phoneCodes.setText(code);

        }
    }
//联网成功调用的方法
    @Override
    public void onSuccess(String result) {
UserBean userBean=new Gson().fromJson(result,UserBean.class);
if (userBean.getCode()==200){
    //成功之后的操作,跳转登录页面
    String msg="注册成功，是否切换到登录界面";
    showDialog(msg);

}else if (userBean.getCode()==201){
    String msg="此手机号码已注册，是否切换到登录界面？";
    showDialog(msg);
return;
}else{
    Toast.makeText(getContext(), "用户注册失败", Toast.LENGTH_SHORT).show();
}
    }

    private void showDialog(String msg) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
        builder.setTitle("提示信息").setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((loginActivity)getActivity()).changePage();
            }
        });
        builder.setNegativeButton("取消",null);
        builder.create().show();
    }

    private boolean checkInfo(String niakname, String tel, String pwd, String repwd) {

        //判断验证码不为空
        if (("").equals(et_phoneCodes.getText().toString())){
            Toast.makeText(getContext(), "验证码不能为空！！！", Toast.LENGTH_SHORT).show();
return false;
        }
            //判断验证码一致性
            if (!et_phoneCodes.getText().toString().equals(code)) {
                Toast.makeText(getContext(), "验证码输入不一致", Toast.LENGTH_SHORT).show();
                et_phoneCodes.setText("");
                return false;

            } else {
                //判断不能为空
                if (TextUtils.isEmpty(niakname) || TextUtils.isEmpty(tel) || TextUtils.isEmpty(pwd) || TextUtils.isEmpty(repwd)) {
                    Toast.makeText(getContext(), "输入内容不能为空，请输入有效字符！！！", Toast.LENGTH_SHORT).show();
                }
                //判断两次输入密码是否相同
                if (!pwd.equals(repwd)) {
                    Toast.makeText(getContext(), "两次输入密码不一致！！！", Toast.LENGTH_SHORT).show();
                    repwdEt.setText("");//将确认密码清空
                    return false;
                }
                //判断电话号码是否符合格式
                String match = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
                if (!tel.matches(match)) {
                    Toast.makeText(getContext(), "输入电话号码格式不匹配！！！", Toast.LENGTH_SHORT).show();
                    telEt.setText("");//将确认密码清空
                    return false;
                }
            }

        return true;
    }
}
